using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using AK.Wwise.TreeView;
using AK.Wwise.Editor;

namespace AudioStudio.Editor
{
	[CustomPropertyDrawer(typeof(AudioEventPC))]
	public class AudioEventPCDrawer : BaseTypeDrawer
    {
        protected override WwiseObjectType WwiseObjectType { get { return WwiseObjectType.Event; } }

        protected override void OnPickerChanged(UnityEditor.SerializedProperty property, AK.Wwise.TreeView.TreeViewItem treeItem)
        {
            Guid guid = Guid.Empty;
            if (treeItem != null)
            {
                guid = new Guid((treeItem.DataContext as AkWwiseTreeView.AkTreeInfo).Guid.ToByteArray());
            }
            var guidProperty = property.FindPropertyRelative("valueGuid.Array");
            AkUtilities.SetByteArrayProperty(guidProperty, guid.ToByteArray());

            var e =AudioUtility.FindEvent(guid);

            if (e != null)
            {
                if (e.Name.EndsWith(AudioManager.PlayerCtrlSuffix))
                {
                    property.FindPropertyRelative("ID_PC").longValue = e.Id;

                    string withoutpc = e.Name.Substring(0, e.Name.Length - AudioManager.PlayerCtrlSuffix.Length);
                    var epc = AudioUtility.FindEvent(withoutpc);
                    if (epc != null)
                    {
                        property.FindPropertyRelative("Id").longValue = epc.Id;
                        property.FindPropertyRelative("Name").stringValue = epc.Name;
                        AkUtilities.SetByteArrayProperty(guidProperty, epc.Guid.ToByteArray());
                    }
                }
                else
                {
                    property.FindPropertyRelative("Id").longValue = e.Id;
                    property.FindPropertyRelative("Name").stringValue = e.Name;

                    var epc = AudioUtility.FindEvent(e.Name + AudioManager.PlayerCtrlSuffix);
                    if (epc != null)
                    {
                        property.FindPropertyRelative("ID_PC").longValue = epc.Id;
                    }
                    else
                    {
                        property.FindPropertyRelative("ID_PC").longValue = 0;
                    }
                }
            }
            else
            {
                property.FindPropertyRelative("Id").longValue = 0;
                property.FindPropertyRelative("Name").stringValue = string.Empty;
            }
            property.serializedObject.ApplyModifiedProperties();
        }

        protected override void AcceptDragDrop(UnityEditor.SerializedProperty property, WwiseObjectReference wof)
        {
            UnityEditor.SerializedProperty vguid = property.FindPropertyRelative("valueGuid.Array");
            if (wof.ObjectName.EndsWith(AudioManager.PlayerCtrlSuffix))
            {
                property.FindPropertyRelative("ID_PC").longValue = wof.Id;

                string withoutpc = wof.ObjectName.Substring(0, wof.ObjectName.Length - AudioManager.PlayerCtrlSuffix.Length);
                var epc = AudioUtility.FindEvent(withoutpc);
                if (epc != null)
                {
                    property.FindPropertyRelative("Id").longValue = epc.Id;
                    property.FindPropertyRelative("Name").stringValue = epc.Name;
                    AkUtilities.SetByteArrayProperty(vguid, epc.Guid.ToByteArray());
                }
            }
            else
            {
                property.FindPropertyRelative("Id").longValue = wof.Id;
                property.FindPropertyRelative("Name").stringValue = wof.ObjectName;
                AkUtilities.SetByteArrayProperty(vguid, wof.Guid.ToByteArray());

                var epc = AudioUtility.FindEvent(wof.ObjectName + AudioManager.PlayerCtrlSuffix);

                if (epc != null)
                {
                    property.FindPropertyRelative("ID_PC").longValue = epc.Id;
                }
                else
                {
                    property.FindPropertyRelative("ID_PC").longValue = 0;
                }
            }
        }
    }
}

